﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class SupportMoveRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<SupportMove> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.SupportMoves
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p => p.SupportName).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public SupportMove GetByID(int ID)
        {
            var result = from p in data.SupportMoves
                         where p.SupportMoveID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<SupportMove> GetByParentID(int ID)
        {
            return data.SupportMoves.Where(p => p.ParentID == ID);
        }
        public IEnumerable<SupportMove> GetByCMND(string cmnd)
        {
            return data.SupportMoves.Where(p => p.Supports.Any(q => q.CMND == cmnd));
        }

        public IEnumerable<SupportMove> GetByProjectID(int ID)
        {
            return data.SupportMoves.Where(p => p.Supports.Any(q => q.ProjectID == ID));
        }

        public bool HasChild(int ID)
        {
            return data.SupportMoves.Any(p => p.ParentID == ID);
        }

        public bool checkSupportMove(int ProjectID, int HouseHouseID, int SupportMoveID)
        {
            return data.Supports.Any(p => p.ProjectID == ProjectID && p.HouseHold.HouseHoldID == HouseHouseID && p.SupportMove.SupportMoveID == SupportMoveID);
        }
        public void Insert(SupportMove sm)
        {
            data.AddToSupportMoves(sm);
        }

        public void DeleteByID(int id)
        {
            try
            {

                var obj = GetByID(id);
                if (obj != null)
                {
                    data.DeleteObject(obj);
                }
            }
            catch
            {
                return;
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}